/* @(#)GenRandNum.java    
 *
 * Project: secure
 *
 * Modify Information:
 * =============================================================================
 *   Author         Date          Description
 *   ------------ ---------- ---------------------------------------------------
 *   Jenny       2005/11/07       first release
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2005 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.util;
import java.security.SecureRandom;
import com.hitrust.gs.util.date.DateUtil;

public class GenRandNum {

    /**
     * 生成随机数字
     * @param bits              随机数位数
     * @return                  生成的随机数
     */
    public static String genRandNum(int aLength) {
        SecureRandom    tRandom     = new SecureRandom();
        long            tLong;
        String          aString     = "";
        tRandom.nextLong();
        tLong = Math.abs(tRandom.nextLong());
        aString = (String.valueOf(tLong)).trim();
        while (aString.length() < aLength){
            tLong = Math.abs(tRandom.nextLong());
            aString += (String.valueOf(tLong)).trim();
        }
        aString = aString.substring(0, aLength);
        
        return aString;
    }
    /**
     * 生成日期时间加指定位数的随机数
     * @param aBits             随机数位数
     * @return                  生成的随机数
     * @throws Exception
     */
    public static String genDTRandNum(int aBits) throws Exception{
        String tCurDT = DateUtil.getCurrentTime("DTM","AD");
        String tRs = "";
        tRs = tCurDT + genRandNum(aBits);
        return tRs;
    }
    /**
     * 生成22位随机数字
     * @return              YYMMDDhhmmSSsssRRRNNNN
     */
    public static String genDTRandNum(){
        StringBuffer tSb = new StringBuffer();
        String tCurDT = "";
        tCurDT = DateUtil.getCurrentTime("DTM","AD2");
        tSb.append(tCurDT);
        tSb.append(genRandNum(3));
        int tHashCode = Thread.currentThread().hashCode();
        String tHCS = Integer.toString(tHashCode);
        tSb.append(tHCS.substring(tHCS.length() - 4));
        return tSb.toString();
    }
    /**
     * 生成传入参数加14位的随机数
     * @param aCustom       传入参数
     * @return
     */
    public static String genCustomRandNum(String aCustom){
        StringBuffer tSb = new StringBuffer(aCustom);
        String tCurTime = "";
        tCurTime = DateUtil.getCurrentTime("TM", "AD");
        tSb.append(tCurTime);
        int tHashCode = Thread.currentThread().hashCode();
        String tHCS = Integer.toString(tHashCode);
        tSb.append(tHCS.substring(tHCS.length() - 5));
        return tSb.toString();
    }
}
